package edu.ucsd.cse110;

import java.util.List;
import java.util.Vector;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;

/**
 * Displays a calendar with log entries.
 */
public class CalendarActivity extends FragmentActivity {
	private class MyOnClickListener implements OnPageChangeListener {
		/**
		 * TODO: Need documentation.
		 */
		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {
		}

		/**
		 * TODO: Need documentation.
		 */
		public void onPageScrollStateChanged(int state) {
			if (state == ViewPager.SCROLL_STATE_IDLE) {
				if (focusedPage == 0) {
					prevMonth.prevMonth();
					currentMonth.prevMonth();
					nextMonth.prevMonth();
					currentMonth.drawCalendar();
					final Handler handler = new Handler();
					handler.postDelayed(new Runnable() {
						/**
						 * @see java.lang.Runnable#run()
						 */
						public void run() {
							mPager.setCurrentItem(1, false);
							prevMonth.drawCalendar();
							nextMonth.drawCalendar();
						}
					}, 100);

				} else if (focusedPage == 2) {
					prevMonth.nextMonth();
					currentMonth.nextMonth();
					nextMonth.nextMonth();
					currentMonth.drawCalendar();
					final Handler handler = new Handler();
					handler.postDelayed(new Runnable() {
						/**
						 * @see java.lang.Runnable#run()
						 */
						public void run() {
							mPager.setCurrentItem(1, false);
							prevMonth.drawCalendar();
							nextMonth.drawCalendar();
						}
					}, 100);
				}
			}
		}

		/**
		 * TODO: Need documentation.
		 */
		public void onPageSelected(int position) {
			focusedPage = position;
		}

	}

	/**
	 * TODO: Need documentation.
	 */
	private class MyPagerAdapter extends FragmentStatePagerAdapter {
		private List<Fragment> fragments;

		/**
		 * TODO: Need documentation.
		 */
		public MyPagerAdapter(FragmentManager fm, List<Fragment> ls) {
			super(fm);
			fragments = ls;
		}

		/**
		 * TODO: Need documentation.
		 */
		@Override
		public int getCount() {
			return 3;
		}

		/**
		 * TODO: Need documentation.
		 */
		@Override
		public Fragment getItem(int position) {
			return fragments.get(position);
		}
	}

	private MonthViewFragment currentMonth;
	private int focusedPage = 1;
	private ViewPager mPager;
	private MyPagerAdapter mPagerAdapter;
	private MonthViewFragment nextMonth;
	private MonthViewFragment prevMonth;

	/**
	 * TODO: Need documentation.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.calendar_main);

		// Cycles the MonthViewFragments among 3 slots in a List.
		// This makes it possible to swipe as many times as the
		// user likes without having to load too much data at one time.
		List<Fragment> fragments = new Vector<Fragment>();
		prevMonth = (MonthViewFragment) Fragment.instantiate(this,
				MonthViewFragment.class.getName());
		prevMonth.prevMonth();

		currentMonth = (MonthViewFragment) Fragment.instantiate(this,
				MonthViewFragment.class.getName());

		nextMonth = (MonthViewFragment) Fragment.instantiate(this,
				MonthViewFragment.class.getName());
		nextMonth.nextMonth();

		// Adding MonthViews to fragments
		fragments.add(prevMonth);
		fragments.add(currentMonth);
		fragments.add(nextMonth);

		// Initializes pager adapter.
		mPagerAdapter = new MyPagerAdapter(getSupportFragmentManager(),
				fragments);

		// Sets the view pager to mPagerAdapter.
		mPager = (ViewPager) findViewById(R.id.mypager);
		mPager.setAdapter(mPagerAdapter);
		mPager.setOnPageChangeListener(new MyOnClickListener());

		// Sets the default page.
		mPager.setCurrentItem(1);
	}
}
